//
//  LuDishDetailViewController.m
//  Delicious
//
//  Created by 魏群 on 16/5/9.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "LuDishDetailViewController.h"
#import "LuDetailModel.h"
#import "LuDetailTableViewCell.h"
#import "IngredientsModel.h"
#import "IngredientsTableViewCell.h"
#import "MakingStepsModel.h"
#import "MakingStepsTableViewCell.h"
#import "LuCommentModel.h"
#import "LuCommentTableViewCell.h"
#import "TableViewFactory.h"

@interface LuDishDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *ludetailArr;
@property (nonatomic, strong) NSMutableArray *ingredientsArr;
@property (nonatomic, strong) NSMutableArray *makingStepsArr;
@property (nonatomic, strong) NSMutableArray *commentsArr;

@property (nonatomic, assign) CGFloat heightForIntroduce;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *backImage;



@end

@implementation LuDishDetailViewController
- (NSMutableArray *)ludetailArr
{
    if (!_ludetailArr)
    {
        _ludetailArr = [NSMutableArray array];
    }
    return _ludetailArr;
}
- (NSMutableArray *)ingredientsArr
{
    if (!_ingredientsArr)
    {
        _ingredientsArr = [NSMutableArray array];
    }
    return _ingredientsArr;
}
- (NSMutableArray *)makingStepsArr
{
    if (!_makingStepsArr)
    {
        _makingStepsArr = [NSMutableArray array];
    }
    return _makingStepsArr;
}
- (NSMutableArray *)commentsArr
{
    if (!_commentsArr)
    {
        _commentsArr = [NSMutableArray array];
    }
    return _commentsArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.backImage = [[UIImageView alloc]initWithFrame:self.view.frame];
    [self.backImage sd_setImageWithURL:[NSURL URLWithString:self.imageUrl]];
    [self.view addSubview:_backImage];
    [self getData];

    

}
- (void)createTableView
{
   
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 4 * 3)];
    headView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = headView;
    [self.tableView registerNib:[UINib nibWithNibName:@"LuDetailTableViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([LuDetailModel class])];
    [self.tableView registerNib:[UINib nibWithNibName:@"IngredientsTableViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([IngredientsModel class])];
    [self.tableView registerNib:[UINib nibWithNibName:@"MakingStepsTableViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([MakingStepsModel class])];
    [self.tableView registerNib:[UINib nibWithNibName:@"LuCommentTableViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([LuCommentModel class])];
    [self.view addSubview:self.tableView];
}
#pragma mark ------ 数据处理 -----------------
- (void)getData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSString *url = [LuDishDetail stringByAppendingString:self.ID];
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *data = responseObject[@"data"];
        LuDetailModel *detailModel = [[LuDetailModel alloc]init];
        [detailModel setValuesForKeysWithDictionary:data];
        self.navigationItem.title = detailModel.name;
        [self.ludetailArr addObject:detailModel];
        
        NSArray *ingredientsArr = data[@"ingredients"];
        for (NSDictionary *dic in ingredientsArr)
        {
            IngredientsModel *model = [[IngredientsModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ingredientsArr addObject:model];
        }
        
        NSArray *makingStepsArr = data[@"makingSteps"];
        
        for (NSDictionary *dic in makingStepsArr)
        {
            MakingStepsModel *model = [[MakingStepsModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.makingStepsArr addObject:model];
            
        }
        
        NSArray *commentArr = data[@"comments"];
        for (NSDictionary *dic in commentArr)
        {
            LuCommentModel *model = [[LuCommentModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.commentsArr addObject:model];
        }
        // 创建tableView
        [self createTableView];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"LuDishDetail=======%@",error);
    }];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else if (section == 1)
    {
        return self.ingredientsArr.count;
    }
    else if (section == 2)
    {
        return self.makingStepsArr.count;
    }
    else
    {
        return self.commentsArr.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0)
    {
        LuDetailModel *model = self.ludetailArr.lastObject;
        LuDetailTableViewCell *cell = (LuDetailTableViewCell *)[TableViewFactory cellWithClass:[LuDetailModel class] tableView:tableView index:indexPath];
        [cell setDataWithModel:model];
        self.heightForIntroduce = [cell heightForText:model.Description];
        return cell;
    }
    else if (indexPath.section == 1)
    {
        IngredientsModel *model = self.ingredientsArr[indexPath.row];
        IngredientsTableViewCell *cell = (IngredientsTableViewCell *)[TableViewFactory cellWithClass:[IngredientsModel class] tableView:tableView index:indexPath];
        [cell setDataWithModel:model];
        return cell;
    }
    else if (indexPath.section == 3)
    {
        LuCommentModel *model = self.commentsArr[indexPath.row];
        LuCommentTableViewCell *cell = (LuCommentTableViewCell *)[TableViewFactory cellWithClass:[LuCommentModel class] tableView:tableView index:indexPath];
        [cell setDataWithModel:model];
        return cell;
    }
    else
    {
        MakingStepsModel *model = self.makingStepsArr[indexPath.row];
        MakingStepsTableViewCell *cell = (MakingStepsTableViewCell *)[TableViewFactory cellWithClass:[MakingStepsModel class] tableView:tableView index:indexPath];
        [cell setDataWithModel:model];
        return cell;
    }
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        return 30;
    }
    else if (indexPath.section == 2)
    {
        return 160;
    }
    else if (indexPath.section == 3)
    {
        return 120;
    }
    else
    {
        return 20 + 10 + 10 + 25 + 10 + 25 + 10 + 35 + self.heightForIntroduce;
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 1)
    {
        return @"食材";
    }
    else if (section == 2)
    {
        return @"步骤";
    }
    else if (section == 3)
    {
        return @"评论";
    }
    else
        return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
